<?php
/**
 * Created by PhpStorm.
 * User: zmit
 * Date: 8/5/17
 * Time: 7:56 PM
 */
namespace ApiBundle\Controller\Platform;

use ApiBundle\Controller\BaseController;

use Symfony\Component\HttpFoundation\Request;

class CompanyController extends BaseController
{
    /*
     * 企业信息列表
     */
    public function companyListAction(Request $request)
    {
        $data['credit_code'] = $request->get("credit_code");
        $data['name'] = $request->get("name");


        $conn = $this->get("database_connection");

        $page = $request->get("page","1");

        $rows = $request->get("rows","10");

        $credit_code = $request->get("credit_code");
        $company_name = $request->get("name");
        $admin_id = $request->get("admin_id");
        $category_id = $request->get("company_category_id");
        $attribute_id = $request->get("company_attribute_id");
        $user_name = $request->get("user_name");


        $sql = "SELECT c.* FROM company c LEFT JOIN user u ON c.id = u.company_id WHERE 1 ";

        // 搜索
        if(!empty($credit_code)){
            $sql .= " AND c.credit_code = '". $credit_code ."' ";
        }

        if(!empty($company_name)){
            $sql .= " AND c.name = '". $company_name ."' ";
        }


        if(!empty($user_name)){
            $sql .= " AND u.technology_liaisons_status = 1 AND u.name = '". $user_name ."'  ";
        }

        if(!empty($admin_id)){
            $sql .= " AND c.admin_id = '". $admin_id ."' ";
        }

        if(!empty($category_id)){
            $sql .= " AND c.company_category_id = '". $category_id ."' ";
        }

        if(!empty($attribute_id)){
            $sql .= " AND c.company_attribute_id = '". $attribute_id ."' ";
        }

        $sql .= " GROUP BY c.id ";

        $sql .= "  ORDER BY c.created_at DESC ";

        $search_all_count = count($conn->fetchAll($sql));

        $all_page = ceil($search_all_count / $rows);

        // 分页
        $m = ($page -1) * $rows;

        $sql .= " LIMIT $m,$rows ";

        $company = $conn->fetchAll($sql);

        foreach($company as &$item){
            $admin = $conn->fetchAssoc("SELECT * FROM admin WHERE id = '". $item["admin_id"] ."'");

            if($admin['admin_role_id'] == 4){
                $item['admin'] = $admin['nickname'];
            } else {
                $item['admin'] = "无";
            }


            $company_attribute = $conn->fetchAssoc("SELECT company_attribute_name FROM company_attribute WHERE  id = '". $item["company_attribute_id"] ."'");

            $company_category = $conn->fetchAssoc("SELECT company_category_name FROM company_category WHERE id = '". $item["company_category_id"] ."' ");


            if($company_attribute){
                $item['company_attribute'] = $company_attribute['company_attribute_name'];
            } else {
                $item['company_attribute'] = "";
            }

            if($company_category){
                $item['company_category'] = $company_category['company_category_name'];
            } else {
                $item['company_category'] = "";
            }



        }

        return $this->jsonResponse(0,"操作成功",array("info" => $company,"search_all_count"=>$search_all_count,"all_page"=>$all_page));
    }

    /*
     * 企业基本信息
     */
    public function companyDetailMessageAction(Request $request)
    {
        $id = $request->get("id");
        if(empty($id)){

            return $this->jsonResponse(1,"参数错误");
        }

        $conn = $this->get("database_connection");
        $data = $conn->fetchAssoc("SELECT c.*, a.nickname as admin_realname, ca.company_attribute_name, cc.company_category_name FROM company c LEFT JOIN admin a ON c.admin_id = a.id LEFT JOIN company_attribute ca ON c.company_attribute_id = ca.id LEFT JOIN company_category cc ON c.company_category_id = cc.id  WHERE c.id = '". $id ."'");

        $is_role_teach = $conn->fetchAssoc("SELECT admin_role_id FROM admin WHERE id = '". $data['admin_id'] ."'");

        if($is_role_teach['admin_role_id'] != 4){
            $data['admin_realname'] = "无";
        }


        return $this->jsonResponse(0,"操作成功",array("info" => $data));

    }

    /*
     * 企业其他信息
     */
    public function companyOtherMessageAction(Request $request)
    {
        $conn = $this->get("database_connection");
        $id = $request->get("id");

        $info['event'] = $conn->fetchAll("SELECT * FROM company_event WHERE company_id = '". $id ."'");

        $info['patent'] = $conn->fetchAssoc("SELECT * FROM company_patent WHERE company_id = '". $id ."'");

        $info['company_medal'] = $conn->fetchAll("SELECT * FROM medal WHERE id IN (SELECT medal_id FROM company_medal WHERE company_id = '". $id ."' ) ");
        $info['id'] = $id;

        $trade_mark = $conn->fetchAssoc("SELECT trade_mark FROM company WHERE id = '". $id ."'");
        $info['trade_mark'] = $trade_mark['trade_mark'];
        // 历史培训消息
          // 获取article_id
        $article = $conn->fetchAll("SELECT * FROM user_article WHERE article_id IN (SELECT id FROM article WHERE admin_id = (SELECT admin_id FROM company WHERE id = '". $id ."') AND training_id IS NOT NULL) AND user_id IN (SELECT id FROM user WHERE company_id = '". $id ."')");


        // 获取 article_ids
        $article_ids = array();
        foreach($article as $item){
            array_push($article_ids,$item['article_id']);
        }

        $article_ids = array_unique($article_ids);
        $training_info = array();
        foreach($article_ids as $item){
            //培训内容
            $training_content = $conn->fetchAssoc("SELECT * FROM training WHERE id = (SELECT training_id FROM article WHERE id = '". $item ."' )");

            // 参加人数
            $user_ids = array();
            foreach ($article as $article_item){

                if($item == $article_item['article_id']){

                    array_push($user_ids,$article_item['user_id']);
                }
            }

            $training_content['article_id'] = $item;

            $training_content['user_num'] = count($user_ids);

            $train_content_info = $conn->fetchAssoc("SELECT title,content FROM article WHERE id = '". $item ."'");
            $training_content['content'] = $train_content_info['content'];
            $training_content['title'] = $train_content_info['title'];

            array_push($training_info,$training_content);
        }

        $info['training_info'] = $training_info;


        // 科技联络人
        $info['technology_contact'] = $conn->fetchAll("SELECT * FROM user WHERE company_id = '". $id ."' AND technology_liaisons_status = 1");


        // 其他联络人
        $info['other_contact'] = $conn->fetchAll("SELECT * FROM user WHERE company_id = '". $id ."' AND technology_liaisons_status = 2");


        return $this->jsonResponse(0,"操作成功",array("info" => $info));

    }


    /*
     * 获取企业的科技联络人
     */
    public function companyTechnologicalAction(Request $request)
    {
        $id = $request->get("id");
        if(!$this->isPlatAdminLogged()){
            return $this->plat_response;
        }

        if(empty($id)){
            return $this->jsonResponse(1,"参数错误");
        }

        $conn = $this->get("database_connection");
        $data = $conn->fetchAll("SELECT * FROM user WHERE company_id = '". $id ."' AND technology_liaisons_status = 1");
        return $this->jsonResponse(0,"操作成功",$data);
    }


}